﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace _04
{
    public class LessonDp : Panel
    {
        public  LessonDp()
        {
            Age = 201;
        }
        //定义，注册，包装
        //1：定义 
        //public readonly DependencyProperty AgeProperty = DependencyProperty.Register(
        //    //2:注册 
        //    "Age",//属性名字
        //         typeof(int),//数据类型
        //         typeof(LessonDp)//类型，该属性属于哪个类型
        //        );
        ////3：包装
        //public int Age
        //{
        //    get { return (int)GetValue(AgeProperty); }
        //    set { SetValue(AgeProperty, value); }
        //}



        public int Age
        {
            get { return (int)GetValue(AgeProperty); }
            set { SetValue(AgeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Age.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty AgeProperty =
            DependencyProperty.Register(
                "Age", 
                typeof(int), 
                typeof(LessonDp),
                new PropertyMetadata(0,OnChanged,OnCorceBack),//默认值
                new ValidateValueCallback(OnValidAge)//验证回调
                );
       

        /// <summary>
        /// 验证回调
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        private static bool OnValidAge(object value)
        {
            int age = (int)value;
            return age <=200;
        }
        /// <summary>
        /// 强制回调
        /// </summary>
        /// <param name="d"></param>
        /// <param name="baseValue"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        private static object OnCorceBack(DependencyObject d, object baseValue)
        {
            int age=(int)baseValue;
            if (age > 200)
            {
                age=200;
            }
            return age;
        }
        /// <summary>
        /// 属性值发生改变
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        /// <exception cref="NotImplementedException"></exception>
        private static void OnChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            //e.OldValue;//旧值
            //e.NewValue//新值
        }
        #region 依赖属性继承  被继承的属性(父)


        public string StuName
        {
            get { return (string)GetValue(StuNameProperty); }
            set { SetValue(StuNameProperty, value); }
        }

        // Using a DependencyProperty as the backing store for StuName.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty StuNameProperty =
            DependencyProperty.Register("StuName", 
                typeof(string),
                typeof(LessonDp),
                new FrameworkPropertyMetadata("",FrameworkPropertyMetadataOptions.Inherits));


        #endregion
    }
}